package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.exception.CustomException;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.*;
import com.itheima.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderService orderService;
    @Override
    public void submit(Order order) {
        long userId = BaseContext.getCurrentId();
        User user = userService.getById(userId);
        String userName = user.getName();
        String phone = user.getPhone();
        Long addressBookId = order.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if(addressBook==null){
            throw new CustomException("用户地址信息有误,不能下单!");
        }
        String consignee = addressBook.getConsignee();
        long orderId = IdWorker.getId();
        String address= (addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                + (addressBook.getDetail() == null ? "" : addressBook.getDetail());
        AtomicInteger amount = new AtomicInteger(0);
        AtomicInteger number = new AtomicInteger(0);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);

        if(shoppingCartList==null||shoppingCartList.size()==0){
            throw new CustomException("购物车为空,不能下单!");
        }

        List<OrderDetail> orderDetailList = shoppingCartList.stream().map(new Function<ShoppingCart, OrderDetail>() {
            @Override
            public OrderDetail apply(ShoppingCart shoppingCart) {
                OrderDetail orderDetail = new OrderDetail();
                String image = shoppingCart.getImage();
                orderDetail.setImage(image);
                String name = shoppingCart.getName();
                orderDetail.setName(name);
                orderDetail.setOrderId(orderId);
                orderDetail.setDishId(shoppingCart.getDishId());
                orderDetail.setSetmealId(shoppingCart.getSetmealId());
                orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
                orderDetail.setNumber(shoppingCart.getNumber());
                orderDetail.setAmount(shoppingCart.getAmount());
                number.addAndGet(shoppingCart.getNumber());
                amount.addAndGet(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())).intValue());
                return orderDetail;
            }
        }).collect(Collectors.toList());
        orderDetailService.saveBatch(orderDetailList);

        order.setUserId(userId);
        order.setUserName(userName);
        order.setOrderTime(LocalDateTime.now());
        order.setCheckoutTime(LocalDateTime.now());
        order.setPhone(phone);
        order.setConsignee(consignee);
        order.setId(orderId);
        order.setNumber(String.valueOf(orderId));
        order.setStatus(2);
        order.setAddress(address);
        order.setAmount(new BigDecimal(amount.get()));
        orderService.save(order);
        shoppingCartService.remove(lqw);

    }
}
